package com.duowei.kitchenshow.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.duowei.kitchenshow.R;
import com.duowei.kitchenshow.adapter.RvBottomAdapter2;
import com.duowei.kitchenshow.adapter.RvLeftAdapter;
import com.duowei.kitchenshow.adapter.RvRightAdapter;
import com.duowei.kitchenshow.bean.Cfpb;
import com.duowei.kitchenshow.bean.Cfpb_csbh;
import com.duowei.kitchenshow.bean.Cfpb_item;
import com.duowei.kitchenshow.event.HttpResponse;
import com.duowei.kitchenshow.httputils.Post;
import com.duowei.kitchenshow.service.Net;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.greenrobot.eventbus.Subscribe;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class Main2Fragment extends BaseMainFragment {

    public static String mCzmc;
    private String inputMsg="";
    public Main2Fragment() {
        // Required empty public constructor
    }

    private int index=0;
    private String fcsbh="";
//    private String sql2="";
    private List<Cfpb> mCfpbList=new ArrayList<>();
    private List<Cfpb_item>mItemList=new ArrayList<>();
    private List<Cfpb_csbh> cfpbCsbhs=new ArrayList<>();
    private RvLeftAdapter mLeftAdapter=new RvLeftAdapter(R.layout.leftadapter_item2,mCfpbList);
    private RvBottomAdapter2 mBottomAdapter2=new RvBottomAdapter2(cfpbCsbhs);

    private RvRightAdapter mRightAdapter=new RvRightAdapter(R.layout.rightadapter_item,mItemList);
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        sql2="select fcsbh,count(*)as sl from jycssz where csmc+',' in(select A.by1 from cfpb A LEFT JOIN JYCSSZ Y ON (Y.CSMC+',')=A.BY1 " +
//                "where A.XDSJ BETWEEN DATEADD(mi,-180,GETDATE()) AND GETDATE() and (isnull(A.sl,0)-isnull(A.tdsl,0))>0 and "+ Net.kitchens+" group by by1)group by fcsbh|";

//        setSql(getSql1(fcsbh)+sql2);
        setSql(getSql1(fcsbh));
        setLeftAdapter(mLeftAdapter);
        setRightAdapter(mRightAdapter);
        setRvBottomAdapter2(mBottomAdapter2);

        mLeftAdapter.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int i) {
                mCzmc = mCfpbList.get(i).getCzmc();
                brushLeftAdapter();
                notifyRightAdapter(mCfpbList.get(i).getItemList());
            }
        });

//        mBottomAdapter2.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() {
//            @Override
//            public void onItemClick(View view, int i) {
//                showLoad();
//                index=0;
//                fcsbh=cfpbCsbhs.get(i).getFcsbh();
//                Post.getInstance().postCfpb(getSql1(fcsbh)+sql2);
//                setSql(getSql1(fcsbh)+sql2);
//            }
//        });
    }

    public void searchCfpb(String input){
        inputMsg=input;
        if(TextUtils.isEmpty(inputMsg)){
//            Post.getInstance().postCfpb(getSql1(fcsbh)+sql2);
            Post.getInstance().postCfpb(getSql1(fcsbh));
            return;
        }
        index=0;
        for(int i=mCfpbList.size()-1;i>=0;i--){
            Cfpb cfpb = mCfpbList.get(i);
            if(!cfpb.getCzmc().contains(inputMsg)){
                mCfpbList.remove(cfpb);
            }
        }
        notifyLeftAdapter(mCfpbList);
    }

    @Subscribe
    public void getHttpResponse(HttpResponse event){
        hideLoad();
        mCfpbList.clear();
        String response = event.getResponse();
        if(response.equals("]]")||response.equals("]")){
            cfpbCsbhs.clear();
            mItemList.clear();
            notifyLeftAdapter(mCfpbList);
            notifyRightAdapter(mItemList);
//            notifyBottomeAdapter2(cfpbCsbhs);
            return;
        }

        setLeftAdapter(response);

//        brushBottomAdapter(response);
    }

    private void brushLeftAdapter() {
        int num=-1;
        for(int j=0;j<mCfpbList.size();j++){
            if(mCzmc.equals(mCfpbList.get(j).getCzmc())){
                num=j;
                mCfpbList.get(j).setClick(true);
            }else{
                mCfpbList.get(j).setClick(false);
            }
        }
        if(num!=-1){
            index=num;
        }else{
            index=0;
        }

        if(TextUtils.isEmpty(inputMsg)){
            notifyLeftAdapter(mCfpbList);
        }else{
            searchCfpb(inputMsg);
        }

        notifyLeftAdapter(mCfpbList);
    }

    private void brushBottomAdapter(String response) {
        String rBack=response.substring(response.lastIndexOf("["),response.length());
        if(rBack.toUpperCase().contains("ERROR")){
            return;
        }
        Type type = new TypeToken<List<Cfpb_csbh>>(){}.getType();
        cfpbCsbhs = new Gson().fromJson(rBack, type);
        cfpbCsbhs.add(0,new Cfpb_csbh("",0,"全部"));
        for(int i=0;i<cfpbCsbhs.size();i++){
            Cfpb_csbh cfpbCsbh = cfpbCsbhs.get(i);
            if(fcsbh.equals(cfpbCsbh.getFcsbh())){
                cfpbCsbh.setClick(true);
            }else{
                cfpbCsbh.setClick(false);
            }
        }
        notifyBottomeAdapter2(cfpbCsbhs);
    }

    private void setLeftAdapter(String response) {
        String rFront=response.substring(0,response.indexOf("]")+1);
        if(rFront.equals("]")){
            mCfpbList.clear();
            mItemList.clear();
            brushLeftAdapter();
            notifyRightAdapter(mItemList);
            return;
        }
        Gson gson = new Gson();
        Cfpb[] cfpbs = gson.fromJson(rFront, Cfpb[].class);
        setCfpb(cfpbs);
        String str = "";
        for(int i=0;i<cfpbs.length;i++){
            Cfpb cfpb=cfpbs[i];
            /**
             * 抓取每个餐桌所包含的菜品数据集
             * */
            for (int j = 0; j < cfpbs.length; j++) {
                if ((cfpbs[j].getBy1()).equals(cfpb.getBy1())) {
                    if(cfpbs[j].getPz().contains("&lt;")&&cfpbs[j].getPz().contains("&gt;")){
                        cfpbs[j].setPz(cfpbs[j].getPz().replaceAll("&lt;", "<").replaceAll("&gt;", ">"));
                    }
                    Cfpb_item cfpb_item = new Cfpb_item(cfpbs[j].getXmbh(),cfpbs[j].getXmmc(),cfpbs[j].getCzmc(),
                            cfpbs[j].getSl(), cfpbs[j].getFzs(),cfpbs[j].getCssj(),cfpbs[j].getXdsj(),cfpbs[j].getYhmc(),
                            cfpbs[j].getXH(), cfpbs[j].getPz(),cfpbs[j].getPos(),cfpbs[j].getDw(),cfpbs[j].getBy10(),cfpbs[j].getXszt());
                    List<Cfpb_item> list = cfpb.getItemList();
                    list.add(cfpb_item);
                    cfpb.setItemList(list);
                }
            }

            if(!str.contains(cfpb.getCzmc())){
                mCfpbList.add(cfpb);
                str+=cfpb.getCzmc()+"-";
            }
        }

        if(TextUtils.isEmpty(mCzmc)&&mCfpbList.size()>0){
            mCzmc=mCfpbList.get(0).getCzmc();
            notifyRightAdapter(mCfpbList.get(0).getItemList());
        }
        brushLeftAdapter();
        notifyRightAdapter(mCfpbList.get(index).getItemList());
    }

    private String getSql1(String fcsbh){
//        if(TextUtils.isEmpty(fcsbh)){
            return "select A.XH,A.wmdbh,A.xmbh,LTrim(A.xmmc)as xmmc,A.dw,(isnull(A.sl,0)-isnull(A.tdsl,0)-isnull(A.YWCSL,0))sl," +
                    "A.pz,CONVERT(varchar(100), a.xdsj, 120)as xdsj,A.BY1 as czmc,datediff(minute,A.xdsj,getdate())fzs,A.yhmc,isnull(A.xszt,'')xszt,A.ywcsl,j.py,isnull(j.by13,9999999)cssj," +
                    "A.by9,A.by10,A.pos,A.by1,A.by2 from cfpb A LEFT JOIN JYXMSZ J ON A.XMBH=J.XMBH " +
                    "where A.XDSJ BETWEEN DATEADD(mi,-180,GETDATE()) AND GETDATE() and (isnull(A.sl,0)-isnull(A.tdsl,0))>0 and "+Net.kitchens+" order by A.xdsj,A.xmmc|";
//        }
//        else{
//            return "select A.XH,A.wmdbh,A.xmbh,LTrim(A.xmmc)as xmmc,A.dw,(isnull(A.sl,0)-isnull(A.tdsl,0)-isnull(A.YWCSL,0))sl,A.pz,CONVERT(varchar(100), a.xdsj, 120)as xdsj,A.BY1 as czmc," +
//                    "datediff(minute,A.xdsj,getdate())fzs,A.yhmc,isnull(A.xszt,'')xszt,A.ywcsl,j.py,isnull(j.by13,9999999)cssj,A.by9,A.by10,A.pos,A.by1,A.by2 " +
//                    "from cfpb A LEFT JOIN JYXMSZ J ON A.XMBH=J.XMBH where A.XDSJ BETWEEN DATEADD(mi,-180,GETDATE()) AND GETDATE() and (isnull(A.sl,0)-isnull(A.tdsl,0))>0 " +
//                    "and "+Net.kitchens+" and A.by1 in(select csmc+',' from jycssz where fcsbh='"+fcsbh+"') order by A.xdsj,A.xmmc|";
//        }
    }
}
